package com.xzxy.xmlg_backend.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

/**
 * WebSocket消息推送服务
 * @author zys
 * @since 2025-06-23
 */
@Service
public class WebSocketService {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    /**
     * 发送消息到指定主题
     * @param topic 主题
     * @param message 消息内容
     */
    public void sendMessage(String topic, Object message) {
        simpMessagingTemplate.convertAndSend(topic, message);
    }

    /**
     * 发送通知消息
     * @param message 消息内容
     */
    public void sendNotification(String message) {
        sendMessage("/topic/stream", message);
    }

    /**
     * 发送JSON格式的通知消息
     * @param title 标题
     * @param content 内容
     * @param type 类型
     */
    public void sendNotification(String title, String content, String type) {
        NotificationMessage notification = new NotificationMessage(title, content, type);
        sendMessage("/topic/stream", notification);
    }

    /**
     * 通知消息实体类
     */
    public static class NotificationMessage {
        private String title;
        private String content;
        private String type;
        private long timestamp;

        public NotificationMessage(String title, String content, String type) {
            this.title = title;
            this.content = content;
            this.type = type;
            this.timestamp = System.currentTimeMillis();
        }

        // Getters and Setters
        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public long getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(long timestamp) {
            this.timestamp = timestamp;
        }
    }
}